<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button icon="el-icon-plus" type="primary" plain @click="add">新增</el-button>
				<!-- <el-button type="danger" v-auth="['manager:users:add']" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button> -->
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.name" placeholder="标题" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" plain @click="getTableList">搜索</el-button>
					<el-button @click="resetQuery" icon="el-icon-refresh">重置</el-button>
				</div>
			</div>
		</el-header>
		
		<el-main class="nopadding">
			<el-table ref="table" v-loading="loading" :data="tableList" @selection-change="selectionChange" border>
				<el-table-column label="#" prop="id" width="80"></el-table-column>
				<el-table-column label="标题" prop="name"></el-table-column>
				<el-table-column label="标识" prop="mark"></el-table-column>
				<el-table-column label="排序" prop="sort"></el-table-column>
				<el-table-column label="更新时间" prop="updated_at" width="180"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<!--<el-popconfirm :title="'确定删除【'+scope.row.title+'】协议数据吗？'" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small">删除</el-button>
								</template>
							</el-popconfirm>-->
						</el-button-group>
					</template>
				</el-table-column>
			</el-table>
		</el-main>
		<el-footer>
			<sc-page v-model="search"></sc-page>
		</el-footer>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'

	export default {
		name: 'platformAgreement',
		components: {
			saveDialog,
		},
		data() {
			return {
				dialog: {
					save: false
				},
				loading: true,
				tableList: [],
				selection: [],
				search: {
					page: 1,
        			pageSize: 20,
					total: 0,
					paging: 0,
        			name: undefined,
				},
				statusLabel: [
					{value: 0, label: '禁用'},
					{value: 1, label: '正常'},
				],
			}
		},
		mounted() {
			this.getTableList()
		},
		watch: {
        	"search.paging": function() {
				this.getTableList()
			},
		},
		methods: {
			//获取列表
			async getTableList() {
				this.loading = true
				var res = await this.$API.platform.agreement.list.get(this.search)
				this.loading = false
				if(res.code == 200) {
					this.tableList = res.data.rows
					this.total = res.data.total
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				} 
			},
			//重置搜索
			resetQuery() {
				this.search = {
					name: undefined
				}
				this.getTableList()
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('add', this.search)
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit', this.search).setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show', this.search).setData(row)
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id}
				var res = await this.$API.platform.xieyi.delete.post(reqData);
				if(res.code == 200){
					this.getTableList()
					this.$message.success("删除成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.getTableList()
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//本地更新数据
			handleSaveSuccess(mode, search){
				if(mode != 'show') {
					this.search = search
					this.getTableList()
				}
			}
		}
	}
</script>

<style>
  .el-row {
    margin-bottom: 20px;
    &:last-child {margin-bottom: 0;}
  }
  .el-col {border-radius: 4px;}
  .bg-purple-dark {background: #99a9bf;}
  .bg-purple {background: #d3dce6;}
  .bg-purple-light {background: #e5e9f2;}
  .grid-content {border-radius: 4px; min-height: 36px;}
  .row-bg {padding: 10px 0; background-color: #f9fafc;}

</style>
